﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNA_Game_Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Basic_XNA
{
	class MainGame : Screen
	{
		Container mapWindow;
		Container sidebar;
		SpriteFont stFont;
		RightClickMenu rightClickMenu;

		public MainGame(Game game, ScreenManager screenManager)
			: base(game, screenManager, "GameScreen")
		{
			stFont = Content.Load<SpriteFont>(@"testFont");

			CreateMapWindow();
			CreateSideBar();
			CreateWindowInWindow();
			ScreenwideInputhandling();
		}

		private void CreateMapWindow()
		{
			Texture2D dropTop = LoadTexture2D("dropmenutopborder");
			Texture2D dropSide = LoadTexture2D("dropmenusideborder");
			Texture2D dropBottom = LoadTexture2D("dropmenubottommenu");
			Texture2D dropBox = LoadTexture2D("dropmenubox");

			mapWindow = this.AddContainer("wndMap", new Vector2(0, 0), new Vector2(500, 480));

			TextBox box = mapWindow.AddTextBox("txtNameBox", new Vector2(20, 20), 150, "textBox");

			mapWindow.AddLabel("lblPrinter", new Vector2(20, 60), "", stFont);

			Label labelRC = mapWindow.AddLabel("lblRC", new Vector2(450, 20), box.Size.Y.ToString(), stFont);

			rightClickMenu = this.AddRightClickMenu(labelRC, "", new string[] { "A", "B", "C", "Main Menu" }, stFont, dropTop, dropSide, dropBottom, dropBox);
			rightClickMenu.GetMenuItem(0).Click += PrintA;
			rightClickMenu.GetMenuItem(1).Click += PrintB;
			rightClickMenu.GetMenuItem(2).Click += PrintC;
			rightClickMenu.GetMenuItem(3).Click += Exit;
		}

		private void CreateSideBar()
		{
			Texture2D background = LoadTexture2D("StressedTin");
			
			sidebar = this.AddContainer("wndSideBar", new Vector2(500, 0), new Vector2(300, 480));
			sidebar.AddTileImage("imgBackground", new Vector2(0, 0), background, new Vector2(300, 480));
			sidebar.AddBorder("brdSideBar", new Vector2(0, 0), new Vector2(300, 480), Border.Sides.Left, "border");
		}

		private void CreateWindowInWindow()
		{
			WindowVar flexWindow = mapWindow.AddWindowVar("wndInWindow", "SubWindow", new Vector2(50, 50), new Vector2(260, 202), "windowTest");
			Button button = flexWindow.Container.AddButton("btnInWindowTestButton", new Vector2(20, 20), "button", "A");
			button.Click += PrintA;
		}

		private void ScreenwideInputhandling()
		{
			this.InputHandler.EscKeyPressed += new InputHandler.KeyPressedEventHandler(ExitPressed);
		}

		public override void ScreenUpdate()
		{
			base.ScreenUpdate();
		} 

		private void PrintA(object sender, EventArgs e)
		{
			Label printer = (Label)this.GetControl("lblPrinter");
			printer.Text += "A";
		}

		private void PrintB(object sender, EventArgs e)
		{
			Label printer = (Label)this.GetControl("lblPrinter");
			printer.Text += "B";
		}

		private void PrintC(object sender, EventArgs e)
		{
			Label printer = (Label)this.GetControl("lblPrinter");
			printer.Text += "C";
		}

		private void Exit(object sender, EventArgs e)
		{
			if (this.GetContainer("wndDialogue") == null)
			{
				Texture2D button = Content.Load<Texture2D>(@"button");
				Texture2D border = Content.Load<Texture2D>(@"blackdot");
				Texture2D background = Content.Load<Texture2D>(@"whitetile");

				this.AddDialogue("wndDialogue", new Vector2(50, 50), "Do you really want to\r\nreturn to the main menu?", DialogueAuto.Buttons.YesNo, "dialogue");
				Button btnYes = (Button)this.GetControl("btnYes@wndDialogue");
				btnYes.Click += ExitClicked;
			}
		}

		private void ExitClicked(object sender, EventArgs e)
		{
			myScreenManager.RemoveScreen();
		}

		private void ExitPressed(object sender, KeyPressedEventArgs e)
		{
			myScreenManager.RemoveScreen();
		}
	}
}
